<template>
	<view style="width: 100vw;display: flex;justify-content: center;">
		<view class="page-content">
			<view style="display: flex;justify-content: space-between;">
				<view style="display: flex;justify-content: start;">
					<view class="add-btn" style="margin-left: 2vh" @click="goBack">
						<span class="btn-text">返回</span>
					</view>
				</view>
				<view style="display: flex;justify-content: end;">
					<view class="add-btn" @click="addUser">
						<span class="btn-text">+新建</span>
					</view>
				</view>
			</view>
			
			<view style="display: flex;justify-content: center;margin-top: 2.8vh;margin-bottom: 2.8vh;">
				<view style="background:rgba(13,46,105, 0.41);border-radius: 8px;width: 50vw;height: 4.4vh">
					<view style="opacity: 1;display: flex;justify-content: space-around;align-items: center;height: 100%;">
						<span class="def-text">ID</span>
						<span class="def-text">用户权限</span>
						<span class="def-text">操作</span>
					</view>
				</view>
			</view>
			<view v-for="(item,index) in userList" style="display: flex;justify-content: center;margin-top: 1vh;">
				<view style="background-image: linear-gradient(90deg, rgba(186,232,255,0.05) 1%, rgba(186,232,255,0.01) 46%, rgba(186,232,255,0.05) 100%);border-radius: 8px;width: 50vw;height: 4.4vh">
					<view style="opacity: 1;display: flex;justify-content: space-around;align-items: center;height: 100%;">
						<span class="def-text">{{ item.userid }}</span>
						<span class="def-text">{{ item.roleid }}</span>
						<view class="def-text">
							<el-popconfirm :title="'是否确认删除' + item.userid" @confirm="deleteUser(item)">
								<span slot="reference" class="def-text" style="color: #3FE5E8;">删除</span>
							</el-popconfirm>
						</view>
					</view>
				</view>
			</view>
			<!-- 弹窗 -->
			<view v-show="diagShow" style="position: absolute;top: 12vh;width: 100%;z-index: 1;">
				<view style="display: flex;justify-content: center;">
					<view style="background-image: url('../../static/imgPc/新建弹窗@2x.png');width: 33.3vw;height: 38vh;background-size: 100% 100%;display: flex;justify-content: center;align-items: center;">
						<view style="display: flex;justify-content: space-around;flex-direction: column;height: 30vh;align-items: center;">
							<view style="display: flex;justify-content: space-around;width: 25vw;align-items: center;">
								<view class="def-text">ID：</view>
								<input v-model="addUserid" class="uni-input" style="text-align: left;padding-left: 15px;width: 21.6vw;height: 4.1vh;background: #004289;border-radius: 8px;" />
							</view>
							<view style="display: flex;justify-content: space-around;width: 25vw;align-items: center;">
								<view class="def-text">权限：</view>
								<el-select :popper-append-to-body="false" style="color: aliceblue;text-align: left;width: 21.6vw;height: 4.1vh;background: rgba(0,136,255,0.28);border-radius: 8px;" v-model="role" placeholder="">
									<el-option v-for="item in roleList" :key="item.value" :label="item.label" :value="item.value">
									</el-option>
								</el-select>
							</view>
							<view style="display: flex;justify-content: space-around;width: 25vw;align-items: center;">
								<view class="def-text">车间：</view>
								<el-select :popper-append-to-body="false" style="color: aliceblue;text-align: left;width: 21.6vw;height: 4.1vh;background: rgba(0,136,255,0.28);border-radius: 8px;" v-model="workshop" placeholder="">
									<el-option v-for="item in workshopList" :key="item.label" :label="item.label" :value="item.label">
									</el-option>
								</el-select>
							</view>
							<view style="display: flex;justify-content: center;">
								<view class="sub-btn" @click="canclepop">
									<span class="btn-text">取消</span>
								</view>
								<view class="sub-btn" @click="submit">
									<span class="btn-text">确定</span>
								</view>
							</view>
						</view>
					</view>
				</view>

			</view>

		</view>
	</view>

</template>

<script>
	import config from '@/config.js'
	import {
		AllUsers,
		AllRoles,
		AllWorkshops,
		Register,
		DeleteUser
	} from '@/api/user.js'
	export default {
		data() {
			return {
				userid: '',
				addUserid: '',
				userList: [],
				diagShow: false,
				roleList: [],
				role: '',
				workshopList: [],
				workshop: '',
				value: ''
			}
		},
		created() {
			this.userid = config.userid
		},
		methods: {
			goBack(){
				console.log('goBack')
				this.$emit('showInfo')
			},
			getAllUsers() {
				AllUsers({
					data: {
						userid: this.userid
					}
				}).then((res) => {
					this.userList = res.data.data
				})
			},
			addUser() {
				this.addUserid = ''
				this.role = ''
				this.workshop = ''
				this.diagShow = true
				AllRoles({
					data: {
						userid: this.userid
					}
				}).then((res) => {
					this.roleList = []
					for (let key in res.data.data) {
						if (res.data.data.hasOwnProperty(key)) { // 确保是对象自身的属性，而非继承的属性
							this.roleList.push({
								label: res.data.data[key],
								value: key
							})
						}
					}
				})
				AllWorkshops({
					data: {
						userid: this.userid
					}
				}).then((res) => {
					this.workshopList = []
					for (let key in res.data.data) {
						if (res.data.data.hasOwnProperty(key)) { // 确保是对象自身的属性，而非继承的属性
							this.workshopList.push({
								label: res.data.data[key],
								value: key
							})
						}
					}
				})
			},
			submit() {
				if(config.roleid>=this.role)
				{
					uni.showToast({
						title: '错误'
					});
					return
				}
				this.diagShow = false
				Register({
					data: {
						userid: this.addUserid,
						password: this.addUserid,
						roleid: this.role,
						workshopid: this.workshop
					}
				}).then((res) => {
					this.getAllUsers()
				})
			},
			canclepop(){
				this.diagShow = false
			},
			deleteUser(user) {
				DeleteUser({
					data: {
						userid: user.userid
					}
				}).then((res) => {
					this.getAllUsers()
				})
			},
			canclepop()
			{
				this.diagShow=false;
			},
		}
	}
</script>

<style scoped>
	/* // 设置下拉框的背景颜色及边框属性； */
	/deep/.el-select-dropdown {
		// 若不将下拉框的背景颜色设置为：transparent，那么做不出来半透明的效果；
		// 因为其最终的显示为：下拉框有一个背景颜色且下拉框的字体有一个背景颜色，重叠后的效果展示；
		/* background-color: transparent; */
		border: none;
	}

	/* // 设置下拉框的字体属性及背景颜色； */
	.el-select-dropdown__item {
		color: #fff;
		background-color: #004289;
	}

	/* // 设置下拉框列表的 padding 值为：0；(即：样式调整) */
	/deep/.el-select-dropdown__list {
		padding: 0;
	}

	/* // 设置输入框与下拉框的距离为：0; (即：样式调整) */
	/deep/.el-popper[x-placement^="bottom"] {
		margin-top: 0px;
	}

	/* // 将下拉框上的小箭头取消；(看着像一个箭头，其实是两个重叠在一起的箭头) */
	/deep/.el-popper .popper__arrow,
	/deep/.el-popper .popper__arrow::after {
		display: none;
	}

	/* // 设置鼠标悬停在下拉框列表的悬停色； */
	.el-select-dropdown__item:hover {
		background-color: rgba(0, 136, 255, 0.8);
	}

	.def-text {
		width: 50%;
		position: relative;
		z-index: 1;
		font-family: SourceHanSansSC-Regular;
		font-size: 16px;
		color: #FFFFFF;
		letter-spacing: 0;
		line-height: 22.4px;
		font-weight: 400;
	}

	.btn-text {
		font-family: SourceHanSansSC-Normal;
		font-size: 16px;
		color: #FFFFFF;
		letter-spacing: 1.6px;
		text-align: center;
		line-height: 26.4px;
		font-weight: 400;
	}

	.add-btn {
		background: #030A28;
		background-image: linear-gradient(180deg, rgba(72, 156, 255, 0.13) 0%, rgba(43, 186, 255, 0.18) 49%, rgba(0, 138, 255, 0.50) 100%);
		box-shadow: inset 0 0 11px 0 #05DCF8;
		width: 7.9vw;
		height: 3.3vh;
		border-radius: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 2vh;
		margin-right: 2vh;
	}

	.sub-btn {
		background: #030A28;
		background-image: linear-gradient(180deg, rgba(72, 156, 255, 0.13) 0%, rgba(43, 186, 255, 0.18) 49%, rgba(0, 138, 255, 0.50) 100%);
		box-shadow: inset 0 0 11px 0 #05DCF8;
		width: 7.9vw;
		height: 3.3vh;
		border-radius: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>